package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.SalaryDao;
import com.zr.model.Employee;
import com.zr.util.JDBCUtil;

public class SalaryDaoImpl implements SalaryDao{

	@Override
	public int CountUserByEid(int eid) {
		int i = 0;
		StringBuffer sql = new StringBuffer("");
		sql.append(" select COUNT(eid) as cs FROM salary ");
		sql.append(" where salary.eid=? ");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, eid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				i=rs.getInt("cs");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	@Override
	public List<Employee> selUserByEid(int eid, int page, int size) {
		List<Employee> user = new ArrayList<Employee>();
		StringBuffer sql = new StringBuffer("");
		sql.append(" select employee.ename,department.dname,salary.first,salary.last,salary.deduct,salary.date ");
		sql.append(" from salary ");
		sql.append(" INNER JOIN employee ");
		sql.append(" ON employee.eid = salary.eid ");
		sql.append(" INNER JOIN department ");
		sql.append(" ON department.did = employee.did ");
		sql.append(" WHERE salary.eid =? ");
		sql.append(" limit ?,? ");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, eid);
			pst.setInt(2, (page-1)*size);
			pst.setInt(3, size);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Employee emp = new Employee();
				emp.setEname(rs.getString("ename"));
				emp.setDepartment(rs.getString("dname"));
				emp.setFirst(rs.getString("first"));
				emp.setDeduct(rs.getString("deduct"));
				emp.setLast(rs.getString("last"));
				emp.setDate(rs.getString("date"));
				user.add(emp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return user;
	}

}
